package com.probiz.estore.imports.util;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.probiz.estore.imports.model.Column;
import com.probiz.estore.imports.model.ImportModel;

public class ImportHelper {
	public static String toReplace(String value,LinkedHashMap<String, String> replaces) {
		if (value != null && replaces!= null
				&& replaces.size() > 0) {
			for (java.util.Map.Entry<String, String> entry : replaces.entrySet()) {
				String regex = entry.getKey();
				String replacement = entry.getValue();
				value = value.replaceAll(regex, replacement);
			}
		}
		return value;
	}
	public static String toReplace(String value, Column column){
		return toReplace(value, column.getReplaces());
	}

	public static String toLowerOrUpper(String value, String toLowerOrUpper) {
		if (StringUtils.isNotEmpty(value)&&StringUtils.isNotEmpty(toLowerOrUpper)) {
			if (toLowerOrUpper.equalsIgnoreCase("Lower")) {
				value = String.valueOf(value).toLowerCase();
			} else if (toLowerOrUpper.equalsIgnoreCase("Upper")) {
				value = String.valueOf(value).toUpperCase();
			} 
		}
		return value;
	}
	public static String toLowerOrUpper(String value, Column column) {
		return toLowerOrUpper(value, column.getToLowerOrUpper());
	}
	
	
	/**
	 * 列值的验证
	 * @param value
	 * @param column
	 * @param model
	 * @return
	 */
	public static String validate(String value,Column column,ImportModel model){
		if(column.isRequired()&&StringUtils.isBlank(value)){
			String msg = "【"+column.getColumnHeader()+"】是必填项,无法导入";
			model.setMsg(msg);
			model.setResult(ImportModel.RESULT_ERROR);
		}
		if (StringUtils.isNotEmpty(value) && column.getMaxLength() != null
				&& column.getMaxLength() > 0) {
			if (value.length() > column.getMaxLength()) {
				model.setMsg("【"+column.getColumnHeader()+"】超过最大长度("+column.getMaxLength()+"),导入失败");
				model.setResult(ImportModel.RESULT_ERROR);
			}
		}
		if (StringUtils.isNotEmpty(value) && column.getEqLength() != null
				&& column.getEqLength() > 0) {
			if (value.length() > column.getEqLength()) {
				model.setMsg("【"+column.getColumnHeader()+"】长度必须小于("+column.getEqLength()+"),导入失败");
				model.setResult(ImportModel.RESULT_ERROR);
			}
		}
		return value;
	}
	
	public static List<String> toSplit(String value, Column column) {
		ArrayList<String> values = new ArrayList<String>();
		if(StringUtils.isNotEmpty(column.getSplit())){
			String tempValues[]=value.split(column.getSplit());
			for (String tempValue : tempValues) {
				values.add(addPrefixAndSuffix(tempValue.trim(), column));
			}
			return values;
		}else{
			values.add(addPrefixAndSuffix(value.trim(), column));
			return values;
		}
	}
	public static List<String> toColumnHeaderSplit(String value, Column column) {
		ArrayList<String> values = new ArrayList<String>();
		if (StringUtils.isNotEmpty(column.getColumnHeaderSplit())) {
			String columnHeaders[] = column.getColumnHeader().split(column.getColumnHeaderSplit());
			for (String columnHeader : columnHeaders) {
				String tempValue=column.getRowDataMap().get(columnHeader);
				values.add(addPrefixAndSuffix(tempValue.trim(), column));
			}
		}else{
			String tempValue=column.getRowDataMap().get(column.getColumnHeader());
			values.add(addPrefixAndSuffix(tempValue.trim(), column));
		}
		return values;
	}
	
	public static String toSelect(String value, Column column) {
		LinkedHashMap<String, String> selects=column.getSelects();
		if (value != null && selects!= null&& selects.size() > 0) {
			value=selects.get(value);
			if(value==null&&StringUtils.isEmpty(column.getDefaultValue())){
				value=selects.entrySet().iterator().next().getValue();
			}
		}
		return value;
	}
	
	public static String addPrefixAndSuffix(String value, Column column) {
		if(StringUtils.isNotEmpty(value)){
			StringBuffer tempValue=new StringBuffer(value);
			if(StringUtils.isNotEmpty(column.getPrefix())){
				tempValue.insert(0,column.getPrefix());
			}
			if(StringUtils.isNotEmpty(column.getSuffix())){
				tempValue.append(column.getSuffix());
			}
			return tempValue.toString();
		}
		return value;
	}
}
